#!/usr/bin/env python3
"""
FR-DM-001行情数据采集功能演示脚本
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from quantcore.data.collectors import MarketDataCollector
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

def demo_basic_usage():
    """演示基本使用方法"""
    print("=== FR-DM-001行情数据采集功能演示 ===\n")
    
    # 创建数据采集器实例
    collector = MarketDataCollector()
    
    print("1. 检查数据最新性...")
    start_date, end_date = collector.check_data_freshness()
    if start_date and end_date:
        print(f"   需要更新数据：{start_date} 到 {end_date}")
    else:
        print("   数据已是最新，无需更新")
    
    print("\n2. 获取股票列表...")
    stock_list = collector.fetch_stock_list()
    if not stock_list.empty:
        print(f"   成功获取{len(stock_list)}只股票")
        print(f"   前3只股票：{stock_list.head(3)[['code', 'name']].to_string()}")
    else:
        print("   获取股票列表失败")
    
    print("\n3. 测试单只股票数据获取...")
    from datetime import date, timedelta
    test_end = date.today()
    test_start = test_end - timedelta(days=3)
    
    stock_data = collector.fetch_daily_prices("000001", test_start, test_end)
    if stock_data is not None and not stock_data.empty:
        print(f"   成功获取上证指数数据，共{len(stock_data)}条记录")
        print(f"   数据字段：{list(stock_data.columns)}")
    else:
        print("   获取上证指数数据失败")
    
    print("\n4. 测试指数数据获取...")
    index_data = collector.fetch_index_daily("000300", test_start, test_end)
    if index_data is not None and not index_data.empty:
        print(f"   成功获取沪深300数据，共{len(index_data)}条记录")
        print(f"   数据字段：{list(index_data.columns)}")
    else:
        print("   获取沪深300数据失败")

def demo_full_data_collection():
    """演示完整数据采集流程"""
    print("\n=== 完整数据采集流程演示 ===\n")
    
    collector = MarketDataCollector()
    
    print("开始执行完整数据采集流程...")
    print("注意：这将根据数据最新性检查结果决定是否采集数据")
    
    # 执行完整的数据采集
    success = collector.fetch_market_data()
    
    if success:
        print("✅ 数据采集完成")
    else:
        print("❌ 数据采集失败或无需更新")

def main():
    """主函数"""
    try:
        # 基本功能演示
        demo_basic_usage()
        
        # 询问是否执行完整数据采集
        print("\n" + "="*50)
        response = input("是否执行完整数据采集流程？(y/n): ").lower().strip()
        
        if response in ['y', 'yes', '是']:
            demo_full_data_collection()
        else:
            print("跳过完整数据采集流程")
        
        print("\n演示完成！")
        
    except KeyboardInterrupt:
        print("\n\n用户中断操作")
    except Exception as e:
        print(f"\n演示过程中出现错误: {e}")
        logging.exception("详细错误信息")

if __name__ == "__main__":
    main()
